package com.lmh.backend.controller;

import com.lmh.backend.entity.News;
import com.lmh.backend.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {
    private NewsService newsService;
    @Autowired
    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public ModelAndView newsHome() {
        ModelAndView mv = new ModelAndView("home");
        List<News> newsList = newsService.getAllNews();
        mv.addObject("newsList", newsList);
        return mv;
    }

    @RequestMapping(value = "/home/{newsId}")
    public ModelAndView newsPage(@PathVariable Long newsId) {
        News news =newsService.getNewsById(newsId);
        ModelAndView mv = new ModelAndView("newsPage");
        mv.addObject("news", news);
        return mv;
    }

    @RequestMapping(value = "/pass/{newsId}")
    public String passNews(@PathVariable Long newsId) {
        newsService.passNews(newsId);
        return "redirect:/admin/checkList";
    }

    @RequestMapping(value = "/delete/{newsId}")
    public String delNews(@PathVariable Long newsId) {
        newsService.delNews(newsId);
        return "redirect:/admin/checkList";
    }
}
